package com.v1.video.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.v1.video.AppContext;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.util.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * 佛教
 * 
 * @author wangfang
 * 
 */
public class MainPageBuddhismFragment extends V1BaseFragment {
	private final String TAG = "MainPageBuddhismFragment";
	private View mRootView;
	private ViewPager mViewPager;
	private FragmentPagerAdapter mAdapter;
	private RelativeLayout mLayPractice;
	private RelativeLayout mLayMonks;
	private List<Fragment> mFragments;
	private TextView mTvPractice;
	private TextView mTvMonks;
	private View line_1;
	private View line_2;
	private View line_3;
	private View line_4;

	public static MainPageBuddhismFragment newInstance(String tag,String genre) {
		MainPageBuddhismFragment f = new MainPageBuddhismFragment();
		Bundle bundle = new Bundle();
		bundle.putString("tag", tag);
		bundle.putString("genre", genre);
		f.setArguments(bundle);
		return f;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mRootView = inflater.inflate(R.layout.fragment_main_page_buddhism,
				container, false);
		initView();
		setListener();
		initData();
		return mRootView;
	}

	private void initView() {
		mViewPager = (ViewPager) mRootView.findViewById(R.id.vp_viewpager);
		mLayPractice = (RelativeLayout) mRootView
				.findViewById(R.id.head_left_back);
		mLayMonks = (RelativeLayout) mRootView
				.findViewById(R.id.head_right_back);
		mTvPractice = (TextView) mRootView.findViewById(R.id.head_left_txt);
		line_1 = (View) mRootView.findViewById(R.id.head_bar_line1);
		line_2 = (View) mRootView.findViewById(R.id.head_bar_line3);
		line_3 = (View) mRootView.findViewById(R.id.head_bar_line5);
		line_4 = (View) mRootView.findViewById(R.id.head_bar_line6);
		mTvMonks = (TextView) mRootView.findViewById(R.id.head_right_txt);
	}
	
	@Override
	public void refresh(Object tag) {
		// TODO Auto-generated method stub
		super.refresh(tag);
	}

	private void setListener() {
		mLayPractice.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setSelectTab(0);
			}
		});
		mLayMonks.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setSelectTab(1);
			}
		});
	}

	private void initData() {
		if (AppContext.getInstance().buddhism_titles != null
				&& AppContext.getInstance().buddhism_titles.size() >= 2) {
			mTvPractice.setText(AppContext.getInstance().buddhism_titles.get(0));
			mTvMonks.setText(AppContext.getInstance().buddhism_titles.get(1));
		} else {
			mTvPractice.setText("正知正见");
			mTvMonks.setText("佛教大德");
		}
		mFragments = new ArrayList<Fragment>();
		mFragments.add(BuddhismFragment.newInstance(Constant.BUDDHISM_TYPE_1));
		mFragments.add(BuddhismFragment.newInstance(Constant.BUDDHISM_TYPE_2));
		mAdapter = new FragmentPagerAdapter(getChildFragmentManager()) {
			@Override
			public int getCount() {
				return mFragments.size();
			}

			@Override
			public Fragment getItem(int arg0) {
				return mFragments.get(arg0);
			}

			@Override
			public int getItemPosition(Object object) {
				// TODO Auto-generated method stub
				return POSITION_NONE;
			}
			
		};
		mViewPager.setAdapter(mAdapter);
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				int currentItem = mViewPager.getCurrentItem();
				setTab(currentItem);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
		setSelectTab(0);
	}

	private void setSelectTab(int index) {
		setTab(index);
		mViewPager.setCurrentItem(index);
	}

	private void setTab(int index) {
		Logger.i(TAG, "当前viewpage的索引=" + index);
		switch (index) {
		case 0:
			mTvPractice.setTextColor(getResources().getColor(
					R.color.color_text_selected));
			line_1.setVisibility(View.VISIBLE);
			line_2.setVisibility(View.GONE);
			line_3.setVisibility(View.GONE);
			line_4.setVisibility(View.VISIBLE);
			mTvMonks.setTextColor(getResources().getColor(
					R.color.color_textColor_black_44));
			break;
		case 1:
			mTvPractice.setTextColor(getResources().getColor(
					R.color.color_textColor_black_44));
			line_1.setVisibility(View.GONE);
			line_2.setVisibility(View.VISIBLE);
			line_3.setVisibility(View.VISIBLE);
			line_4.setVisibility(View.GONE);
			mTvMonks.setTextColor(getResources().getColor(
					R.color.color_text_selected));
			break;
		}
	}
}
